<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>选择会计科目</title>
		<%@ include file="/WEB-INF/jsp/include.jsp"%>
		<script type="text/javascript">
		var sbcs_id = "${sbcs_id}";
		var zt_id = "${zt_id}";
		var zt_name = "${zt_name}";
		var ssnd = "${ssnd}";
		
		var multiSelect = <c:choose><c:when test='${param.multiSelect == "true"}'>true</c:when><c:otherwise>false</c:otherwise></c:choose>;
		
		$(function(){
			setAjaxLoading();
			
			$("#sel_zt").combobox({
				onSelect: function(record) {
					zt_id = record.value;
					sel_zt_onChange(zt_id);
				}
			});
			
			//绑定报表页签panel的OnResize事件
			var west_panel = $("#div_container").layout("panel", "center");
			var panel_options = west_panel.panel("options");
			panel_options.onResize = resizeTree;
			
			resizeTree();
			
			$("#sel_zt").combobox("setValue", zt_id);
			sel_zt_onChange(zt_id);
		});

		function sel_zt_onChange(zt_id) {
			$.fn.zTree.destroy("tree");
			initTree();
		}
		
		function initTree() {
			var setting = {
				callback: {
					onClick: null
				},
				async: {
					enable: true,
					url: "${path}/jcxx/kmxx/queryTreeData",
					dataType: "json",
					autoParam: ["level=jc","id=sj_id","lb_dm"],
					otherParam: {"zt_id": zt_id, "ssnd": ssnd},
					dataFilter: ajaxDataFilter
				},
				check: {
					enable: multiSelect
				},
				data: {
					simpleData: {
						enable: false,
						idKey: "id",
						pIdKey: "pId",
						rootPId: null
					},
					key: {
						name: "title"
					}					
				},
				edit: {
					enable: false					
				},
				view: {
					selectedMulti: false
				}
			};
			
			$.fn.zTree.init($("#tree"), setting, null);
       	}
				
		function ajaxDataFilter(treeId, parentNode, responseData) {
		    var nodes = responseData.returnObject.nodes;
		    for (var i=0; i<nodes.length; i++) {
		    	var node = nodes[i];

		    	if (parentNode != null) {
		    		node.lb_dm = parentNode.lb_dm
		    	} else {
		    		node.lb_dm = node.code;
		    	}
		    	
		    	if (multiSelect) {
		    		node.nocheck = (node.isParent == "true" || node.isParent == true);
		    	}
		    }
		    return nodes;
		};
		
		//调整报表页签树宽高
		function resizeTree(w, h) {
			var width = $("#tree").parent().width();
			var height = $("#tree").parent().height();
			
			$("#tree").width(width);
			$("#tree").height(height);
		}
		
		function btnOk() {
			var treeObj = $.fn.zTree.getZTreeObj("tree");
			if (multiSelect) {
				var nodes = treeObj.getCheckedNodes(true);
				if (nodes == null || nodes.length == 0) {
					//$.messager.alert('提示','请至少选择一个会计科目。','info',function(){});
					$.alert("pick", "info", "会计科目");
					return;
				}
									
				var arr = new Array();
				for (var i=0; i<nodes.length; i++) {
					var node = nodes[i];
					
					var item = new Object();
					item.zt_id = zt_id;
					item.zt_name = zt_name;
					item.ssnd = ssnd;
					item.kjkm_id = node.id;
					item.kjkm_code = node.code;
					item.kjkm_qc = node.name;
					
					arr.push(item);
				}
	
				parent.window.callback_selectZzsKjkm(arr);
				setTimeout("closeLayer()",100);
			} else {
				var nodes = treeObj.getSelectedNodes();
				if (nodes == null || nodes.length == 0) {
					//$.messager.alert('提示','请选择一个会计科目。','info',function(){});
					$.alert("pick", "info", "会计科目");
					return;
				}
				
				var node = nodes[0];
				if (node.isParent) {
					//$.messager.alert('提示','请选择末级会计科目。','info',function(){});
					$.alert("pick", "info", "末级会计科目");
					return;
				}
				
				var returnValue = new Object();
				returnValue.zt_id = zt_id;
				returnValue.zt_name = zt_name;
				returnValue.ssnd = ssnd;
				returnValue.kjkm_id = node.id;
				returnValue.kjkm_code = node.code;
				returnValue.kjkm_qc = node.name;
				
				parent.window.callback_selectZzsKjkm(returnValue);
				setTimeout("closeLayer()",100);
			}
		}
		</script>
	</head>
  <body  style="margin:0px; padding:0px;" >
  <%@ include file="/WEB-INF/jsp/loading.jsp" %>
	<div id="div_container" class="easyui-layout" fit="true" border="false">
		<div data-options="region:'north',title:'',split:false,border:true" style="height:35px;">
			<div style="padding: 5px;">
				<span>数据源：</span>
				<select id="sel_zt" class="easyui-combobox" style="width:214px;" data-options="editable:false">
					<c:forEach var="item"  items="${sjdyList}">
						<option value="${item.id}">${item.name}</option>
					</c:forEach>
				</select>
			</div>
		</div>	
		<div data-options="region:'center',title:'',split:false,border:true">
			<ul id="tree" class="ztree" style="margin-top: 0px; padding: 0px; border-width: 0px; width:250px;overflow:auto;"></ul>
		</div>
		<div data-options="region:'south',title:'',split:false,border: false" style="height:55px;">
			<div align="center" style="margin-top: 15px;">
	        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="btnOk()">确定</a>
	        	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="closeLayer();">取消</a>
       		</div>
		</div>
	</div>	
</body>
</html>
